package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealImpl implements SetMealService {

    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage ();
        Integer pageSize = queryPageBean.getPageSize ();
        String queryString = queryPageBean.getQueryString ();
        PageHelper.startPage ( currentPage, pageSize );
        Page<Setmeal> page = setMealDao.findPage ( queryString );
        List<Setmeal> result = page.getResult ();
        long total = page.getTotal ();
        return new PageResult ( total, result );
    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkGroupIds) {
        setMealDao.add ( setmeal );
        if (checkGroupIds.length > 0 && checkGroupIds != null) {
            Map<String, Integer> map = new HashMap<> ();
            for (Integer checkGroupId : checkGroupIds) {
//                Map<String, Integer> map = new HashMap<> ();
                 map.put ( "setmeal_id", setmeal.getId () );
                map.put ( "checkgroup_id", checkGroupId );
                setMealDao.setSetMealAndCheckGroup ( map );
            }
        }
        //把图片名称加入redis缓存
        jedisPool.getResource ().sadd ( RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg () );

    }

}





